package com.thd.springboottest.stopwatch.controller;

import org.springframework.stereotype.Controller;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * com.thd.springboottest.stopwatch.controller.StopWatchController
 *
 * @author: wanglei62
 * @DATE: 2022/7/4 9:26
 **/
@Controller
@RequestMapping("/stopwatch")
public class StopWatchController {

    @RequestMapping("/test01")
    @ResponseBody
    // url http://127.0.0.1:8899/thd/stopwatch/test01
    public String test01() throws Exception{
        StopWatch sw = new StopWatch();
        sw.start("A");
        Thread.sleep(1000);
        sw.stop();
        System.out.println(sw.getLastTaskTimeMillis()); // 本次start 到 stop用时
        System.out.println(sw.getTotalTimeMillis());// 总用时

        System.out.println("==========================");
        sw.start("B");
        Thread.sleep(1000);
        sw.stop();
        System.out.println(sw.getLastTaskTimeMillis());// 本次start 到 stop用时
        System.out.println(sw.getTotalTimeMillis());// 总用时


        System.out.format("%32s|%10d|%-16s", "a",123, "cccc");

        /*
getTotalTimeSeconds() 获取总耗时秒，同时也有获取毫秒的方法
prettyPrint() 优雅的格式打印结果，表格形式
shortSummary() 返回简短的总耗时描述
getTaskCount() 返回统计时间任务的数量
getLastTaskInfo().getTaskName() 返回最后一个任务TaskInfo对象的名称
         */
        return "SUCCESS";
    }

    @RequestMapping("/test")
    @ResponseBody
    // url http://127.0.0.1:8899/thd/stopwatch/test
    public String test() throws Exception{
        StopWatch sw = new StopWatch();
        sw.start("A");
        Thread.sleep(1000);
        sw.stop();
        sw.start("B");
        Thread.sleep(2000);
        sw.stop();
        sw.start("C");
        Thread.sleep(3000);
        sw.stop();


        StopWatch sw2 = new StopWatch();
        sw2.start("A");
        Thread.sleep(1000);
        sw2.stop();
        sw2.start("B");
        Thread.sleep(1000);
        sw2.stop();
        System.out.println(sw.prettyPrint());
        System.out.println(sw2.prettyPrint());
        return "SUCCESS";
    }
}
